<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <title>${siteName!""}|商品分类管理-编辑物品分类</title>
<#include "../common/header.ftl"/>

</head>

<body>
<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!--左侧导航-->
        <aside class="lyear-layout-sidebar">

            <!-- logo -->
            <div id="logo" class="sidebar-header">
                <a href="index.html"><img src="/admin/images/logo-1.png" title="${siteName!""}" alt="${siteName!""}" /></a>
            </div>
            <div class="lyear-layout-sidebar-scroll">
        <#include "../common/left-menu.ftl"/>
            </div>

        </aside>
        <!--End 左侧导航-->

    <#include "../common/header-menu.ftl"/>

        <!--页面主要内容-->
        <main class="lyear-layout-content">

            <div class="container-fluid">

                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-header"><h4>编辑物品分类</h4></div>
                            <div class="card-body">
                                <form action="add" id="category-add-form" method="post" class="row">
                                    <input type="hidden" name="id" value="${category.id}">
                                    <div class="form-group col-md-12">
                                        <label>上传icon</label>
                                        <div class="form-controls">
                                            <ul class="list-inline clearfix lyear-uploads-pic">
                                                <li class="col-xs-4 col-sm-3 col-md-2">
                                                    <figure>
                                       <#if category.icon?length gt 0>
                                     <img src="/photo/view?filename=${category.icon}" id="show-picture-img">
                                         <#else>
                    		         <img src="/admin/images/default-head.jpg" id="show-picture-img" alt="默认头像">
                                              </#if>
                                                    </figure>
                                                </li>
                                                <input type="hidden" name="icon" id="icon" value="${category.icon}">
                                                <input type="file" id="select-file" style="display:none;"
                                                       onchange="upload('show-picture-img','icon')">
                                                <li class="col-xs-4 col-sm-3 col-md-2">
                                                    <a class="pic-add" id="add-pic-btn" href="javascript:void(0)"
                                                       title="点击上传"></a>
                                                </li>
                                            </ul>
                                        </div>
                                    </div>
                                    <div class="input-group m-b-10">
                                        <span class="input-group-addon">分类名称</span>
                                        <input type="text" class="form-control required" id="name" name="name" value="${category.name}" placeholder="请输入分类名称" tips="请填写用分类名称" />
                                    </div>
                                    <div class="input-group m-b-10">
                                        <span class="input-group-addon">所属分类</span>
                                        <select name="parentId" class="form-control" id="category">
                    	             <#if parentList??>
                                    <option value="">父类分类</option>
                    		           <#list parentList as parent>
                    		      <option value="${parent.id}" <#if category.parent??><#if parent.id == category.parentId>selected</#if></#if>>${parent.name}</option>
                                      </#list>
                                  </#if>
                                        </select>
                                    </div>

                                    <div class="form-group col-md-12">
                                        <button type="button" class="btn btn-primary ajax-post" id="edit-form-submit-btn">确 定</button>
                                        <button type="button" class="btn btn-default" onclick="javascript:history.back(-1);return false;">返 回</button>
                                    </div>
                                </form>

                            </div>
                        </div>
                    </div>

                </div>

            </div>

        </main>
        <!--End 页面主要内容-->
    </div>
</div>
<#include "../common/footer.ftl"/>
<script type="text/javascript" src="/admin/js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="/admin/js/main.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        //提交按钮监听事件
        $("#edit-form-submit-btn").click(function(){
            if(!checkForm("category-add-form")){
                return;
            }
            var name = $("#name").val();
            if (name.trim().length<2 ){
                showErrorMsg('用户名长度不能少于2!');
                return;
            }
            if (name.trim().length>18){
                showErrorMsg('用户名长度不能大于18');
                return;
            }

            var data = $("#category-add-form").serialize();
            $.ajax({
                url:'edit',
                type:'PUT',
                data:data,
                dataType:'json',
                success:function(data){
                    if(data.code == 0){
                        showSuccessMsg('分类编辑成功!',function(){
                            window.location.href = 'list';
                        })
                    }else{
                        showErrorMsg(data.msg);
                    }
                },
                error:function(data){
                    alert('网络错误!');
                }
            });
        });
        //监听上传图片按钮
        $("#add-pic-btn").click(function(){
            $("#select-file").click();
        });
    });

</script>
</body>